﻿using System.Net.Http.Headers;
using System.Text;
// 需要单独安装

namespace _20_HttpClient;

internal class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string City { get; set; }

    public override string ToString()
    {
        return $"{Name} - {Age} - {City}";
    }
}

internal class Contributor
{
    public string Login { get; set; }
    public short Contributions { get; set; }

    public override string ToString()
    {
        return $"{Login,20}: {Contributions} contributions";
    }
}

internal class Program
{
    private static async Task Main(string[] args)
    {
        #region 1_HttpClient Head请求, 获取Header信息

        //// Head请求, 获取Header信息
        //var url = "http://webcode.me";
        //using var client = new HttpClient();
        //var result = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
        //Console.WriteLine(result);

        #endregion

        #region 2_HttpClient Post请求

        //var person = new Person { Name = "张三", Age = "18", City = "北京" };
        //var json = JsonConvert.SerializeObject(person); // 先序列化
        //var data = new StringContent(json, UTF8Encoding.UTF8, "application/json"); // 转为JSON字符串

        //var url = "https://httpbin.org/post";
        //using var client = new HttpClient();

        //var responese = await client.PostAsync(url, data); // POST请求

        //// 解析响应
        //string result = responese.Content.ReadAsStringAsync().Result;
        //Console.WriteLine(result);

        #endregion

        #region 3_HttpClient JSON请求

        //using var client = new HttpClient();

        //client.BaseAddress = new Uri("https://api.github.com");
        //client.DefaultRequestHeaders.Add("User-Agent", "C# console program");
        //client.DefaultRequestHeaders.Accept.Add(
        //        new MediaTypeWithQualityHeaderValue("application/json"));

        //var url = "repos/symfony/symfony/contributors";
        //HttpResponseMessage response = await client.GetAsync(url);
        //response.EnsureSuccessStatusCode();
        //var resp = await response.Content.ReadAsStringAsync();

        //List<Contributor> contributors = JsonConvert.DeserializeObject<List<Contributor>>(resp);
        //contributors.ForEach(Console.WriteLine);

        #endregion

        #region 4_HttpClient下载图像

        //using var httpClient = new HttpClient();
        //var url = "https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/weather/icons/a2.png";
        //byte[] imageBytes = await httpClient.GetByteArrayAsync(url);

        //string documentsPath = Environment.GetFolderPath(
        //        Environment.SpecialFolder.MyPictures);

        //string localFilename = "favicon.ico";
        //// 合并路径、文件名
        //string localPath = Path.Combine(documentsPath, localFilename);
        //File.WriteAllBytes(localPath, imageBytes);

        #endregion

        #region 5_HttpClient基本身份验证

        var userName = "user7"; // 正确为user7
        var passwd = "passwd"; // 正确为passwd
        var url = "https://httpbin.org/basic-auth/user7/passwd";

        using var client = new HttpClient();

        var authToken = Encoding.ASCII.GetBytes($"{userName}:{passwd}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
            Convert.ToBase64String(authToken));

        var result = await client.GetAsync(url);

        var content = await result.Content.ReadAsStringAsync();
        Console.WriteLine(content);

        #endregion
    }
}